import '../../../../core/app_export.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../widgets/subscription_item_view.dart';
import 'subscriptions_cubit.dart';
import 'package:flutter/material.dart';

class SubscriptionsScreen extends StatelessWidget {
  const SubscriptionsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    var subscriptionsCubit = SubscriptionsCubit();
    return Scaffold(
      appBar:_buildAppBar(context:context ,cubit: subscriptionsCubit),
      body: BlocProvider<SubscriptionsCubit>(
        create: (context) => subscriptionsCubit..init(),
        child: BlocConsumer<SubscriptionsCubit, SubscriptionsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = SubscriptionsCubit.get(context);

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      InkWell(
                        borderRadius: BorderRadius.circular(32),
                        onTap: (){
                          cubit.type = null;
                          cubit.getAllSubscriptions();
                        },
                        child: Container(
                          margin:const EdgeInsets.all(4),
                          padding:const EdgeInsets.all(8),
                          decoration:BoxDecoration(
                            color: Theme.of(context).colorScheme.secondary,
                            borderRadius: BorderRadius.circular(99),
                            border:cubit.type== null? Border.all(color: Colors.deepOrange):null,
                          ),
                          child: Text('الكل',style: theme.textTheme.labelMedium,),
                        ),
                      ),
                      InkWell(
                        borderRadius: BorderRadius.circular(32),
                        onTap: (){
                          cubit.type = 1;
                          cubit.getAllSubscriptions();
                        },
                        child: Container(
                          margin:const EdgeInsets.all(4),
                          padding:const EdgeInsets.all(8),
                          decoration:BoxDecoration(
                            color: Theme.of(context).colorScheme.secondary,
                            borderRadius:BorderRadius.circular(99),
                            border:cubit.type== 1? Border.all(color: Colors.deepOrange):null,
                          ),
                          child: Text('الفعالة',style: theme.textTheme.labelMedium,),
                        ),
                      ),
                      InkWell(
                        borderRadius: BorderRadius.circular(32),
                        onTap: (){
                          cubit.type = 0;
                          cubit.getAllSubscriptions();
                        },
                        child: Container(
                          margin:const EdgeInsets.all(4),
                          padding:const EdgeInsets.all(8),
                          decoration:BoxDecoration(
                            color: Theme.of(context).colorScheme.secondary,
                            borderRadius: BorderRadius.circular(99),
                            border:cubit.type== 0? Border.all(color: Colors.deepOrange):null,
                          ),
                          child:  Text('الغير فعالة',style: theme.textTheme.labelMedium,),
                        ),
                      ),
                    ],
                  ),
                  Expanded(
                    child: ScreenStateWidget(
                      isDone: state is SubscriptionsDone,
                      isError: state is SubscriptionsError,
                      isLoading: state is SubscriptionsLoading,
                      onRefresh: cubit.getAllSubscriptions,
                      onDone: ListView.builder(
                        itemCount:cubit.subscriptions.length,
                        itemBuilder: (context,index){
                          return SubscriptionItemView(subscription:cubit.subscriptions[index]);
                        },
                      ),
                    ),
                  ),
                ],
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context, required SubscriptionsCubit cubit}) {
    return CustomAppBar(
        actions: [

        ],
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "الاشتراكات", margin: EdgeInsets.only(left: 12.h)));
  }

}
